package com.fhz.exam001.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fhz.exam001.R;
import com.fhz.exam001.entity.Food;

import java.util.List;

/**
 * 时间:2022/4/25
 *
 * @author msfeng
 * 简述: TODO
 */
public class FoodAdapter extends BaseAdapter {

    private Context context;
    private List<Food.Data> list ;

    public FoodAdapter(Context context, List<Food.Data> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 每一行的布局
     * @param position
     * @param convertView
     * @param parent
     * @return
     *
     *
     * 1,第一个优化match
     * 2,
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.food_layout, null);
            viewHolder.img = convertView.findViewById(R.id.food_img);
            viewHolder.name = convertView.findViewById(R.id.food_name);
            convertView.setTag(viewHolder);
        }

        viewHolder = (ViewHolder) convertView.getTag();
        Food.Data data = list.get(position);
        viewHolder.name.setText(data.getTitle());

        return convertView;
    }

    public class ViewHolder{
        ImageView img;
        TextView name;
    }
}

